/**
 * Project Name: mms-provider-user
 * File Name: EmployeeController
 * Package Name: com.scb.dev.mms.controller
 * Date: 2018/11/27 12:08 PM
 * Copyright (c) 2018, Wang, Haoyue All Rights Reserved.
 */
package com.scb.dev.mms.controller;

import com.scb.dev.mms.common.DataCommon;
import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.pojo.Employee;
import com.scb.dev.mms.service.IEmployeeService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * ClassName: EmployeeController
 * Description: Employee Controller
 * Date: 2018/11/27 12:08 PM
 *
 * @author Wang, Haoyue
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@Api(value = "EmployeeController", description = "employee API")
@RestController
@RequestMapping("/sm/employee/")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * addEmployee: add employee
     *
     * @param employee
     * @return
     */
    @ApiOperation(value = "addEmployee", notes = "Use this API to add a new Employee.")
    @RequestMapping(value = "employee", method = RequestMethod.POST)
    public ResponseResult addEmployee(@RequestBody Employee employee, HttpSession session) {
        log.info("Employee Controller -> addEmployee 添加职员");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return employeeService.insertEmployee(employee, accountId);
    }

    /**
     * deleteEmployee: delete employee
     *
     * @param employeeId
     * @return
     */
    @ApiOperation(value = "deleteEmployee", notes = "Use this API to delete a employee.")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "employeeId", value = "employee id", required = true, dataType = "String"),
    })
    @RequestMapping(value = "employee/{employeeId}", method = RequestMethod.DELETE)
    public ResponseResult deleteEmployee(@PathVariable("employeeId") String employeeId, HttpSession session) {
        log.info("Employee Controller -> deleteEmployee 删除职员");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return employeeService.deleteEmployee(employeeId, accountId);
    }

    /**
     * updateEmployee: update employee
     *
     * @param employee
     * @return
     */
    @ApiOperation(value = "updateEmployee", notes = "Use this API to update a employee.")
    @RequestMapping(value = "employee", method = RequestMethod.PUT)
    public ResponseResult updateEmployee(@RequestBody @ApiParam(name = "employee", value = "employee entity") Employee employee, HttpSession session) {
        log.info("Employee Controller -> updateEmployee 更新职员");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return employeeService.updateEmployee(employee, accountId);
    }

    /**
     * findOne: get a employee by id
     *
     * @param employeeId
     * @return
     */
    @ApiOperation(value = "findOne", notes = "Use this API to find a employee by id.")
    @ApiImplicitParam(paramType = "path", name = "employeeId", value = "employee id", required = true, dataType = "String")
    @RequestMapping(value = "employee/{employeeId}",method = RequestMethod.GET)
    public ResponseResult findOne(@PathVariable("employeeId") String employeeId, HttpSession session) {
        log.info("Employee Controller -> findOne 查询一个职员");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return employeeService.queryOne(employeeId);
    }

    /**
     * findAll: get all employees
     *
     * @return
     */
    @ApiOperation(value = "findAll", notes = "Use this API to find all employees' details.")
    @RequestMapping(value = "employee", method = RequestMethod.GET)
    public ResponseResult findAll(HttpSession session) {
        log.info("Employee Controller -> findAll 查询所有职员");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return employeeService.queryAll();
    }

    /**
     * findAllEmployee: get all employees list
     *
     * @return
     */
    @ApiOperation(value = "findAll", notes = "Use this API to find all employees' details.")
    @RequestMapping(value = "employeeNickname", method = RequestMethod.GET)
    public ResponseResult findAllEmployee(HttpSession session) {
        log.info("Employee Controller -> findAllEmployee 查询所有职员Map");
        String accountId = (String) session.getAttribute(DataCommon.CURRENT_ACCOUNT_ID);
        if(StringUtils.isBlank(accountId)){
            return new ResponseResult(false, ResultType.USER_NOT_LOGIN, StatusCode.FAULT_FLAG);
        }
        return employeeService.queryAllEmployee();
    }


    @ApiOperation(value = "setDefaultRoleId", notes = "Use this API to set the employees' roleId to defaultRoleId.")
    @RequestMapping(value = "/setDefaultRoleId/{roleId}", method = RequestMethod.PUT)
    public ResponseResult setDefaultRoleId(@PathVariable("roleId")String roleId) {
        log.info("Employee Controller -> updateRoleId 更新roleId");
        return employeeService.setDefaultRoleId(roleId);
    }

}
